package com.name.givename.dao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DinamicProxy {


    public static void main(String[] args) {
        Sayhello sayhello = new Sayhello();
        InvocationHandler handler = new MyInvocationHandler(sayhello);
        Hello proxyInstance = (Hello) Proxy.newProxyInstance(Sayhello.class.getClassLoader(),Sayhello.class.getInterfaces(),handler);
        proxyInstance.sayHello();
    }

    interface  Hello {
        void sayHello();
    }

    static class Sayhello implements Hello {

       public Sayhello () {}
        @Override
        public void sayHello() {
            System.out.println("我是反射动态代理执行的实际方法 hello");
        }
    }

   static class MyInvocationHandler implements InvocationHandler{

        private Object target;

        public MyInvocationHandler (Object target) {
            this.target = target;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("反射机制代理生效中*********");
            Object result = method.invoke(target,args);
            return result;
        }
    }




}
